var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
  
    noramalData: [{
        "Cover": "http://php.bkybk.com/static/images/wx/pic4.png",
        "CoverHeight": 267,
        "CoverWidth": 350,
        "title": "标题标题标题标题标题标题标题标题标题标题标题标题"
      },
      {
        "Cover": "http://php.bkybk.com/static/images/wx/pic1.png",
        "CoverHeight": 471,
        "CoverWidth": 672,
        "title": "标题标题"
      },
      {
        "Cover": "https://bkimg.cdn.bcebos.com/pic/0ff41bd5ad6eddc47879259433dbb6fd536633c6?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UxODA=,g_7,xp_5,yp_5",
        "CoverHeight": 471,
        "CoverWidth": 672,
        "title": "标题标题"
      },
      {
        "Cover": "https://bkimg.cdn.bcebos.com/pic/9a504fc2d562853540043eba99ef76c6a6ef6341?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2UyMjA=,g_7,xp_5,yp_5",
        "CoverHeight": 471,
        "CoverWidth": 672,
        "title": "标题标题"
      },
      {
        "Cover": "https://bkimg.cdn.bcebos.com/pic/e4dde71190ef76c6a818bc979716fdfaae516747?x-bce-process=image/resize,m_lfit,w_268,limit_1/format,f_jpg",
        "CoverHeight": 671,
        "CoverWidth": 672,
        "title": "标题标题"
      },
      {
        "Cover": "https://bkimg.cdn.bcebos.com/pic/9825bc315c6034a81a33dc5acc13495408237613?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U5Mg==,g_7,xp_5,yp_5",
        "CoverHeight": 871,
        "CoverWidth": 672,
        "title": "标题标题"
      },
      {
        "Cover": "http://dashus.oss-cn-shenzhen.aliyuncs.com/DefaultImage/Game/20190313090409/完美9.png",
        "CoverHeight": 871,
        "CoverWidth": 672,
        "title": "标题标题"
      },{
        "Cover": "http://dashus.oss-cn-shenzhen.aliyuncs.com/DefaultImage/Game/20190306144842/1001.png",
        "CoverHeight": 467,
        "CoverWidth": 350,
        "title": "标题标题"
      },{
        "Cover": "http://dashus.oss-cn-shenzhen.aliyuncs.com/DefaultImage/Game/20190306144842/1001.png",
        "CoverHeight": 467,
        "CoverWidth": 350,
        "title": "标题标题"
      },
      {
        "Cover": "http://dashus.oss-cn-shenzhen.aliyuncs.com/DefaultImage/Game/20190313090409/完美9.png",
        "CoverHeight": 467,
        "CoverWidth": 672,
        "title": "标题标题"
      }
    ],
    url: 'listing',
    leftList: [],
    rightList: [],
    leftHight: 0,
    rightHight: 0
  },
    //以本地数据为例，实际开发中数据整理以及加载更多等实现逻辑可根据实际需求进行实现   
  onLoad: function(options) {
    this.getDataList();
    this.setData({picUrl: app.globalData.picUrl});
  },

  //获取数据信息
  getDataList: function(page){
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + this.data.url,
      method: 'POST',
      data: {page: page},
      header: {
        'ConTent-Type': 'application/xml'
      },
      success: function(date){
        if(date.data.code == 200){
          console.log(date.data.data.articleList)
          if(date.data.data.articleList){
            that.data.noramalData = date.data.data.articleList;
            that.setPicListData();
          }
        }
      }
    })
  },

  setPicListData: function(){
    var that = this;
    var allData = that.data.noramalData;
    //定义两个临时的变量来记录左右两栏的高度，避免频繁调用setData方法
    var leftH = that.data.leftHight;
    var rightH = that.data.rightHight;
    var leftData = [];
    var rightData = [];
    console.log(allData)
    console.log('2:'+this.data.noramalData)
    for (let i = 0; i < allData.length; i++) {
      var currentItemHeight = parseInt(Math.round(allData[i].CoverHeight * 345 / allData[i].CoverWidth));
      allData[i].CoverHeight = currentItemHeight + "rpx";//因为xml文件中直接引用的该值作为高度，所以添加对应单位
      if (leftH == rightH || leftH < rightH) {//判断左右两侧当前的累计高度，来确定item应该放置在左边还是右边
        leftData.push(allData[i]);
        leftH += currentItemHeight;
      } else {
        rightData.push(allData[i]);
        rightH += currentItemHeight;
      }
    }
  
    //更新左右两栏的数据以及累计高度
    that.setData({
      leftHight: leftH,
      rightHight: rightH,
      leftList: leftData,
      rightList: rightData
    })
  },

  onJumpDetail: function(event){
    var id = event.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../detail/pic-detail/pic-detail?id=' + id
    })
  },

})